package com.shenzhen.horselive.widget;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.api.ApiException;
import com.shenzhen.horselive.common.utils.Utils;
import com.shizhefei.mvc.ILoadViewFactory;


public class MyLoadViewFactory implements ILoadViewFactory {

    @Override
    public ILoadMoreView madeLoadMoreView() {
        return new LoadMoreHelper();
    }

    @Override
    public ILoadView madeLoadView() {
        return new LoadViewHelper();
    }

    public static class LoadMoreHelper implements ILoadMoreView {

        protected TextView footView;

        protected OnClickListener onClickRefreshListener;
        private int textColor = 0;

        @Override
        public void init(FootViewAdder footViewHolder, OnClickListener onClickRefreshListener) {
            footView = (TextView) footViewHolder.addFootView(R.layout.layout_listview_foot);
            if (textColor != 0) {
                footView.setTextColor(textColor);
            }
            this.onClickRefreshListener = onClickRefreshListener;
            showNormal();
        }

        @Override
        public void showNormal() {
            footView.setText("点击加载更多");
            footView.setOnClickListener(onClickRefreshListener);
        }

        @Override
        public void showLoading() {
            footView.setText("正在加载中..");
            footView.setOnClickListener(null);
        }

        @Override
        public void showFailed(Throwable exception) {
            if(exception instanceof ApiException){
                footView.setText(exception.getMessage());
                if(((ApiException) exception).getErrorCode()==110){
                    showReLogin(footView.getContext(),exception);
                }
            }else {
                footView.setText("加载失败，点击重新加载");
            }
            footView.setOnClickListener(onClickRefreshListener);
        }


        @Override
        public void showNomore() {
            footView.setText("我是有底线的");
            footView.setOnClickListener(null);
        }

        public void setTextColor(int color) {
            if (footView != null) {
                footView.setTextColor(color);
            }
            this.textColor = color;
        }

        public void setFooterVisibility(int visibility) {
            footView.setVisibility(visibility);
        }
    }

    public static class LoadViewHelper implements ILoadView {
        //		private VaryViewHelper helper;
        private ContentViewHolder holder;
        private Context context;
        private View emptyView;
        private String emptyText;
        private boolean scrollable=false;

        public void setScrollable(boolean scrollable) {
            this.scrollable = scrollable;
        }

        @Override
        public void init(View switchView, OnClickListener onClickRefreshListener) {
            this.context = switchView.getContext().getApplicationContext();
            holder = new ContentViewHolder(context,scrollable);
            holder.setContent(switchView);
            if (emptyView != null) {
                holder.setEmptyView(emptyView);
            }
            if (emptyText != null) {
                holder.setNoDataPrompt(emptyText);
            }
            holder.setRetryListener(onClickRefreshListener);
        }

        @Override
        public void showContent() {
            holder.showContent();
        }

        @Override
        public void showLoading() {
            holder.showLoading();
        }

        @Override
        public void onLoadMoreError(Throwable exception) {
            //Toast.makeText(context, "网络加载失败", Toast.LENGTH_SHORT).show();
        }

        public void showQuietLoading() {
            holder.showQuietLoading();
        }

        @Override
        public void showFailed(Throwable exception) {
            holder.showRetry(exception);
            if(exception instanceof ApiException){
                if(((ApiException) exception).getErrorCode()==ApiException.AUTHORIZATION_ERROR){
                    showReLogin(context,exception);
                }
            }
        }

        @Override
        public void showEmpty() {
            holder.showEmpty();
        }

        public void setEmptyView(View emptyView) {
//			return holder;
            if (holder != null) {
                holder.setEmptyView(emptyView);
            }
            this.emptyView = emptyView;
        }

        public void setEmptyText(String emptyText) {
            if (holder != null) {
                holder.setNoDataPrompt(emptyText);
            }
            this.emptyText = emptyText;
        }
    }

    private  static void showReLogin(Context context,Throwable ex) {
        if(context instanceof FragmentActivity) {
            Utils.showError((FragmentActivity) context, ex);
        }
    }
}
